﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 提供给按layerMask判断，指定进入的layer视为碰撞计算
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BoundingBase<T> : MonoBehaviour
{
    public LayerMask layerMask;
    public List<T> InBoundingList { get; set; }
    public event Action<T> OnTriggerEnter;
    public event Action<T> OnTriggerExit;
    protected void Start()
    {
        InBoundingList = new List<T>();
    }

    protected void OnEnter(Collider2D collider2D)
    {
        int offset = layerMask.value >> collider2D.gameObject.layer;//与运算移动比较的位
        offset = offset & 1;
        if (offset == 1)
        {
            var component = collider2D.GetComponent<T>();
            InBoundingList.Add(component);
            OnTriggerEnter?.Invoke(component);
        }
    }
    protected void OnExit(Collider2D collider2D)
    {
        int offset = layerMask.value >> collider2D.gameObject.layer;
        offset = offset & 1;
        if (offset == 1)
        {
            var component = collider2D.GetComponent<T>();
            InBoundingList.Remove(component);
            OnTriggerExit?.Invoke(component);
        }
    }
}
